﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win.UltraWinDataSource;
using Infragistics.Win.UltraWinGrid;

namespace Pasta.Medio.Dialogs
{
	public partial class PrintLabelsDialog : DialogBase, IScannerHost
	{
		private enum PrintTask
		{
			Labels,
			Plugin
		}

		private PrintTask _task = PrintTask.Labels;
		private IScannerPlugin _plugin;
		private bool _select = false;

		private Enums.Printing.State _result = Enums.Printing.State.Skip;
		private Dictionary<string, int> _plateIndex = new Dictionary<string, int>();

		#region Lifetime                - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		public PrintLabelsDialog( bool select = false ) : base( false )
		{
			_task = PrintTask.Labels;
			_select = select;

			InitializeComponent();
		}

		/// <summary>
		/// Constructor for printing labels through the plugin approach
		/// </summary>
		/// <param name="services"></param>
		public PrintLabelsDialog( IScannerPlugin plugin, bool select = false )
		{
			_task = PrintTask.Plugin;
			_plugin = plugin;
			_select = select;

			InitializeComponent();
		}

		/// <summary>
		/// Refresh the view called by OnLoad
		/// </summary>
		protected override void RefreshView()
		{
			base.RefreshView();

			Mute = true;

			if( _task == PrintTask.Plugin )
				PreparePluginPage();
			else if( _task == PrintTask.Labels )
				PreparePrintPage();

			Mute = false;
		}

		private void PreparePluginPage()
		{
			wizardTabs.Tabs["Plugin"].Selected = true;
			ok.Text = "Next >";
			ok.Enabled = true;

			Control control = _plugin.Control;
			wizardTabs.Tabs["Plugin"].TabPage.Controls.Add( control );
			control.Dock = DockStyle.Fill;
			_plugin.InitializePlugin( this );
		}

		/// <summary>
		/// Prepare the printing options page
		/// </summary>
		private void PreparePrintPage()
		{
			wizardTabs.SelectedTab = wizardTabs.Tabs["Print"];
			ok.Text = "Skip >";
			ok.Enabled = true;

			if( _task == PrintTask.Plugin )
			{
				_plugin.ScanFolder( String.Empty );
			}

			printControl.Initialize( null, _select );
		}

		#endregion Lifetime             - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		#region Event Handling          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		/// <summary>
		/// Called by the embedded print control to notify status change.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="state"></param>
		private void PrintJobChange( object sender, Enums.Printing.State state )
		{
			switch( state )
			{
			case Enums.Printing.State.Printed:
				ok.Enabled = false;
				break;
			case Enums.Printing.State.Success:
//				ok.Text = "Finish >";
//				ok.Enabled = true;
				_result = state;
				PrepareFinishPage();
				break;
			case Enums.Printing.State.Fail:
				ok.Text = "Skip >";
				ok.Enabled = true;
				_result = state;
				break;
			}
		}

		/// <summary>
		/// Dialog OK button clicked, function depends on wizard page.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ok_Click( object sender, EventArgs e )
		{
			switch( wizardTabs.SelectedTab.Key )
			{
			case "Plugin":
				PreparePrintPage();
				break;
			case "Print":
				PrepareFinishPage();
				break;
			default: // Finish
				Close( DialogResult.OK );
				break;
			}
		}

		#endregion Event Handling       - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		#region Methods                 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		/// <summary>
		/// Prepare the finish and submit page
		/// </summary>
		private void PrepareFinishPage()
		{
			// switch the tab and configure the buttons
			wizardTabs.SelectedTab = wizardTabs.Tabs["Finish"];
			ok.Text = "Ok";
			ok.Enabled = true;
			cancel.Enabled = false;

			string printText = String.Format(
				"The {0} label(s) were created successfully<br/><br/>", 0 );

			if( _result == Enums.Printing.State.Success )
			{
				//picState.Image = Properties.Resources.Ok_48;
				printText +=
					"The printing of the barcode labels was also successful.<br/><br/>" +
					"Remember to affix the labels to the corresponding items.";
			}
			else
			{
				//picState.Image = Properties.Resources.Warning_48;
				if( _result == Enums.Printing.State.Fail )
				{
					printText += "However, the label printing failed.<br/>" +
						"Resolve the issue and try again.";
				}
				else if( _result == Enums.Printing.State.Skip )
				{
					printText += "However, you skipped the actual label printing.";
				}
			}

			flResult.Value = printText;
		}

		public void SetTitle( String title )
		{
			Title = title;
		}

		public void SetText( String text )
		{
			Text = text;
		}

		public void EnableOk( bool enable )
		{
			ok.Enabled = enable;
		}

		#endregion Methods              - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	}
}
